﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;

namespace IntelligentCMS
{
    public partial class CMSContentTypeConfig : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //Label_Info.Text = "ID: " + id;

            if (!this.IsPostBack)
            {
                CMSDbDataLayer fields = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
                fields.AddQuery("SELECT [Name], [ID] FROM Field");
                fields.SelectQuery = true;
                fields.Execute();

                for (int i = 0; i < fields.Data.Rows.Count; i++)
                {
                    DropDownList_Fields.Items.Add(new ListItem(common.FromBase64String(fields.Data.Rows[i]["Name"].ToString()), fields.Data.Rows[i]["ID"].ToString()));

                }

            }
            else
            {
                
            }
            if (id == -1)
            {
                Button_AddField.Visible = false;
                DropDownList_Fields.Visible = false;

            }
            else
            {

                RefreshGrid();

                //Execute();
                //Button_AddField.Visible = true;
                //DropDownList_Fields.Visible = true;
            }

        }

        private void RefreshGrid()
        {


           

            CMSDbDataLayer fields = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
            fields.AddQuery("SELECT Field.[Name], Field.[Description], ContentTypeFields.[ID] AS [Delete] FROM Field INNER JOIN ContentTypeFields ON Field.ID = ContentTypeFields.FieldID WHERE ContentTypeFields.ContentTypeID = " + id);
            fields.SelectQuery = true;
            fields.Execute();

            List<CMSColumnType> newCMSColumnTypes = new List<CMSColumnType>();
            CMSColumnType CMSColumnType_Name = new CMSColumnType(CMSColumnType.CMSCOLUMNTYPE_Text, string.Empty, string.Empty);
            CMSColumnType CMSColumnType_Description = new CMSColumnType(CMSColumnType.CMSCOLUMNTYPE_Text, string.Empty, string.Empty);
            CMSColumnType CMSColumnType_Delete = new CMSColumnType(CMSColumnType.CMSCOLUMNTYPE_Hyperlink, "managefields.aspx?ID={0}", "delete");

            newCMSColumnTypes.Add(CMSColumnType_Name);
            newCMSColumnTypes.Add(CMSColumnType_Description);
            newCMSColumnTypes.Add(CMSColumnType_Delete);


            for (int i = 0; i < fields.Data.Rows.Count; i++)
            {
                //fields.Data.Rows[i]["ID"] = newDataLayer.Data.Rows[i]["ID"].ToString();
                fields.Data.Rows[i]["Name"] = common.FromBase64String(fields.Data.Rows[i]["Name"].ToString());
                fields.Data.Rows[i]["Description"] = common.FromBase64String(fields.Data.Rows[i]["Description"].ToString());
                //fields.Data.Rows[i]["Edit"] = newDataLayer.Data.Rows[i]["ID"].ToString();
                fields.Data.Rows[i]["Delete"] = fields.Data.Rows[i]["Delete"].ToString();

            }


            //newDataLayer.Data.Columns["Link"].ColumnName = string.Empty;


            theCMSDataGrid.Data = fields.Data;
            theCMSDataGrid.CMSColumnTypes = newCMSColumnTypes;
            theCMSDataGrid.Execute();
            //theCMSDataGrid.Execute();

        }


        private long id = -1;


        /// <summary>
        /// ID of ContentType we are editing.
        /// </summary>
        public long ContentTypeID
        {
            get
            {
                return id;
            }

            set
            {
                id = value;
            }

        }



        public void Execute()
        {
            // Get any saved data and display it.

            CMSDbDataLayer contenttype = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
            contenttype.AddQuery("SELECT [Name], [Description], [Tag] FROM ContentType WHERE ID = " + id);
            contenttype.SelectQuery = true;
            contenttype.Execute();


            TextBox_Name.Text = common.FromBase64String(contenttype.Data.Rows[0]["Name"].ToString());
            TextBox_Description.Text = common.FromBase64String(contenttype.Data.Rows[0]["Description"].ToString());
            TextBox_Tag.Text = common.FromBase64String(contenttype.Data.Rows[0]["Tag"].ToString());


        }

        protected void Button_AddField_Click(object sender, EventArgs e)
        {
            
            CMSDbDataLayer addfield = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
            addfield.AddQuery("INSERT INTO [ContentTypeFields] ([ContentTypeID], [FieldID], [DateTime]) VALUES (" + id + ", " + DropDownList_Fields.SelectedValue + ", GETDATE())");
            addfield.SelectQuery = false; // Insert != Select, so it's false.
            addfield.Execute();

            RefreshGrid();

            Label_Info.Text = "Field added.";

           

        }

        protected void Button_Close_Click(object sender, EventArgs e)
        {

        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {

            // check for no input
            if (TextBox_Name.Text.Trim() == string.Empty || TextBox_Description.Text.Trim() == string.Empty || TextBox_Tag.Text.Trim() == string.Empty)
            {
                Label_Info.Text = "No textbox may be empty.";
                return;
            }



            // Insert
            if (id == -1)
            {


                CMSDbDataLayer insertcontent = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
                insertcontent.AddQuery("INSERT INTO [ContentType] ([Name], [Description], [DateTime], [Tag]) VALUES ('" + common.GetBase64String(TextBox_Name.Text.Trim()) + "', '" + common.GetBase64String(TextBox_Description.Text.Trim()) + "', GETDATE(), '" + common.GetBase64String(TextBox_Tag.Text.Trim()) + "')");
                insertcontent.SelectQuery = false;
                insertcontent.Execute();


                CMSDbDataLayer contenttypeid = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
                contenttypeid.AddQuery("SELECT [ID] FROM ContentType WHERE [Name] = '" + common.GetBase64String(TextBox_Name.Text.Trim()) + "'");
                contenttypeid.SelectQuery = true;
                contenttypeid.Execute();

                id = long.Parse(contenttypeid.Data.Rows[0]["ID"].ToString());
            }
            else
            {
                // Update

            }

            Label_Info.Text = "Field successfully saved.";

        }

        protected void Button_Test_Click(object sender, EventArgs e)
        {
            Label_Info.Text = "This should fire.";
            Response.Redirect("http://www.microsoft.com");
        }
    }
}